package com.qf.controller;

import com.qf.TryException.TryException;
import com.qf.common.R;
import com.qf.entity.TbSeckillGoods;
import com.qf.service.GoodsService;
import com.qf.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author sky
 * @date 2022-08-19
 */
@Controller
public class GoodController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 根据时间查询在秒杀期内的商品
     * @param request
     * @return
     */
    public R findGoodsByDate(HttpServletRequest request){
        String strDate = request.getParameter("date");
        Date date = DateUtil.strToUtilDate(strDate);
        List<TbSeckillGoods> tbSeckillGoods = goodsService.queryGoodsByTime(date);
        return new R("200","success",tbSeckillGoods);
    }

    /**
     * 根据要购买的商品id更新库存
     * @param request
     * @return
     */
    public R BuyGoodById(HttpServletRequest request) {
        String strId = request.getParameter("id");
        Long id = Long.parseLong(strId);
        TbSeckillGoods goodsByid = goodsService.findGoodsByid(id);
        try {
            goodsService.updateNum(goodsByid.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new R("200","success",null);
    }


}
